<?php
include(__DIR__ . '/config.php');

use PhpAmqpLib\Connection\AMQPStreamConnection;
use PhpAmqpLib\Message\AMQPMessage;

$exchange_name = 'topic_logs';
$queue_name = 'topic_logs';

$connection = new AMQPStreamConnection(HOST,PORT,USER,PASS,VHOST);

$channel = $connection->channel();

$channel->exchange_declare($exchange_name, 'topic', false, false, false);

// 将第三个参数设置为true，表示将队列持久化
$channel->queue_declare($queue_name,false,true,false,false);

$routing_key = isset($argv[1]) && !empty($argv[1]) ? $argv[1] : 'anonymous.info';

$data = implode(' ', array_slice($argv, 2));

if (empty($data)) $data = "Hello World!";

$msg = new AMQPMessage($data);

$channel->basic_publish($msg, 'topic_logs', $routing_key);

echo " [x] Sent ", $routing_key, ':', $data, " \n";

$channel->close();
$connection->close();

